package com.sample.javabean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.AreaForm;
import com.sample.action.form.BilldetailForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;


/**
 * @author梁桂花
 *
 * 2018-6-10
 */
public class Billdetail {
	private static final Log LOG=LogFactory.getLog(Billdetail.class);
	private String billNo;
	private String mbNo;
	private String cardNo;
	private String allCost;
	private String bxpay;
	private String treattime;
	private String recodtime;
	private String treateInNo;
	private String treatAgenName;
	private JdbcUtils jdbcUtils;
	
	/**
	 * 不带参数的构造函数
	 */
	public Billdetail(){
		jdbcUtils =new JdbcUtils();
	}
	
	
	public List<Billdetail> findAll(){
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		List<Billdetail> billdetailList=new ArrayList<Billdetail>(20);
		
		try{
			conn=jdbcUtils.getConn();
			stmt=conn.createStatement();
			String sql="select *from t_billdetail where 1=1";
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				String billNo=rs.getString("billNo");
				String mbNo=rs.getString("mbNo");
				String cardNo=rs.getString("cardNo");
				String allCost =rs.getString("allCost");
				String bxpay =rs.getString("bxpay");
				String treattime=rs.getString("treattime");
				String recodtime=rs.getString("recodtime");
				String treateInNo =rs.getString("treateInNo");
				String treatAgenName =rs.getString("treatAgenName");
				
				Billdetail model=new Billdetail(billNo,mbNo,cardNo,allCost,bxpay,treattime,recodtime,treateInNo,treatAgenName);
				
				billdetailList.add(model);
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("加载慢病报销登记失败", e);
		}finally{
			jdbcUtils.close(conn, stmt);
		}
		return billdetailList;
		
		
	}
	/**加载列表
	 * @throws Exception
	 */
	public void load()throws Exception{
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try{
			conn=jdbcUtils.getConn();
			stmt=conn.createStatement();
			String sql="select * from t_billdetail where cardNo='"+this.cardNo+"'";
			rs=stmt.executeQuery(sql);
			if(rs.next()){
				
				this. billNo=rs.getString("billNo");
				this. mbNo=rs.getString("mbNo");
				this.cardNo=rs.getString("cardNo");
				this.allCost =rs.getString("allCost");
				this.bxpay =rs.getString("bxpay");
				this.treattime=rs.getString("treattime");
				this.recodtime=rs.getString("recodtime");
				this. treateInNo =rs.getString("treateInNo");
				this.treatAgenName =rs.getString("treatAgenName");
			
			}
		}catch (SQLException e) {
			e.printStackTrace();
			throw new SQLException("加载列表");
		} finally {
			jdbcUtils.close(conn, stmt);
		}
		
	}
	
	/**
	 * 修改慢病登记信息
	 * @author 梁桂花
	 * 2018.6.19
	 * 
	 */
	public void editBilldetail(){

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 插入SQL语句
			String sql = "update  t_billdetail set cardNo='"+this.cardNo+"',mbNo='"+this.mbNo+"',cardNo='"+this.cardNo+"',allCost='"+this.allCost+"',bxpay='"+this.bxpay+"',treattime='"+this.treattime+"',recodtime='"+this.recodtime+"',treateInNo='"+this.treateInNo+"',treatAgenName='"+this.treatAgenName+"' where cardNo='"+this.cardNo+"'";
			// 执行SQL语句，并返回结果集
			stmt.executeUpdate(sql);
			// 关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	public void delBilldetail() {

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 插入SQL语句
			String sql = "delete from t_billdetail  where cardNo='"+this.cardNo+"'";
			//执行SQL语句并且返回结果集
			System.out.println("删除慢病登记信息");
			stmt.executeUpdate(sql);
			//关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	public void add(){
		Connection conn =null;
		Statement stmt=null;
		try{
			//������Ӷ���	
			 conn =jdbcUtils.getConn();
			//����SQL
			String sql="insert into t_billdetail(billNo,mbNo,cardNo,allCost,bxpay,treattime,recodtime,treateInNo, treatAgenName) values('"+this.billNo+"','"+this.mbNo+"','"+this.cardNo+"','"+this.allCost+"','"+this.bxpay+"','"+this.treattime+"','"+this.recodtime+"','"+this.treateInNo+"','"+this.treatAgenName+"')";
			//Statement����SQL������ݿ�
			 stmt =conn.createStatement();
			stmt.executeUpdate(sql);
		}catch (Exception e) {
			e.printStackTrace();
			
		}finally{
			jdbcUtils.close(conn, stmt);
			
		}
		
	}
	public Pager<Billdetail> list(int page,int size,BilldetailForm form) {

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null;
		Pager<Billdetail> results=new Pager<Billdetail>();
		results.setPage(page);
		results.setSize(size);
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			int start=(page-1)*size;
			int limit=size;
			//��ѯSQL
			String selectSql="select * from t_billdetail where 1=1";
			if(form!=null&&form.getCardNo()!=null&&!"".equals(form.getCardNo())){
				selectSql +=" and cardNo like'%"+form.getCardNo()+"%'";
			}
			
			//ͳ��SQL
			String countSql ="select count(*)as c from ("+selectSql+") as t1";
			//��ҳSQL
			 String pageSql=selectSql+" limit "+start+","+limit;
			 rs= stmt.executeQuery(countSql);
			 int total=0;
			 while(rs.next()){
				 total=rs.getInt("c");
			 }
			 results.setTotal(total);
			 //��ѯ��ҳ��Ϣ
			 rs= stmt.executeQuery(pageSql);
			 while(rs.next()){
			
			
				  String billNo=rs.getString("billNo");
				  String mbNo=rs.getString("mbNo");
				  String cardNo=rs.getString("cardNo");
				  String allCost=rs.getString("allCost");
				  String bxpay=rs.getString("bxpay");
				  String treattime=rs.getString("treattime");
				  String recodtime=rs.getString("recodtime");
				  String treateInNo=rs.getString("treateInNo");
				  String treatAgenName=rs.getString("treatAgenName");
				
				  Billdetail model=new Billdetail();
				  model.setBillNo(billNo);
				  model.setMbNo(mbNo);
				  model.setCardNo(cardNo);
				  model.setAllCost(allCost);
				  model.setBxpay(bxpay);
				  model.setTreattime(treattime);
				  model.setRecodtime(recodtime);
				  model.setTreateInNo(treateInNo);
				  model.setTreatAgenName(treatAgenName);
				  
		
				  results.getDataSets().add(model);
			 }
			 results.init();
			// 5:�ر���Դ
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}
		return results;

	}
	
		public Billdetail( String billNo, String mbNo, String cardNo,String allCost,String bxpay, String treattime,String recodtime,String treateInNo,String treatAgenName){
			super();
			this. billNo=billNo;
			this. mbNo=mbNo;
			this.cardNo=cardNo;
			this.allCost =allCost;
			this.bxpay =bxpay;
			this.treattime=treattime;
			this.recodtime=recodtime;
			this. treateInNo =treateInNo;
			this.treatAgenName =treatAgenName;
			
		}


		public String getBillNo() {
			return billNo;
		}


		public void setBillNo(String billNo) {
			this.billNo = billNo;
		}


		public String getMbNo() {
			return mbNo;
		}


		public void setMbNo(String mbNo) {
			this.mbNo = mbNo;
		}


		public String getCardNo() {
			return cardNo;
		}


		public void setCardNo(String cardNo) {
			this.cardNo = cardNo;
		}


		public String getAllCost() {
			return allCost;
		}


		public void setAllCost(String allCost) {
			this.allCost = allCost;
		}


		public String getBxpay() {
			return bxpay;
		}


		public void setBxpay(String bxpay) {
			this.bxpay = bxpay;
		}


		public String getTreattime() {
			return treattime;
		}


		public void setTreattime(String treattime) {
			this.treattime = treattime;
		}


		public String getRecodtime() {
			return recodtime;
		}


		public void setRecodtime(String recodtime) {
			this.recodtime = recodtime;
		}


		public String getTreateInNo() {
			return treateInNo;
		}


		public void setTreateInNo(String treateInNo) {
			this.treateInNo = treateInNo;
		}


		public String getTreatAgenName() {
			return treatAgenName;
		}


		public void setTreatAgenName(String treatAgenName) {
			this.treatAgenName = treatAgenName;
		}
		 

	/*public void add() {
		Connection conn =null;
		Statement stmt=null;
		try{
			//������Ӷ���	
			 conn =jdbcUtils.getConn();
			//����SQL
			String sql="insert into t_billdetail(billNo,mbNo,cardNo,allCost,bxpay,treattime,recodtime,treateInNo,treatAgenName) " +
					"values('"+this.billNo+"','"+this.mbNo+"','"+this.cardNo+"','"+this.allCost+"','"+this.bxpay+"'" +
							",'"+this.treattime+"','"+this.recodtime+"','"+this.treateInNo+"','"+this.treatAgenName+"')";
			//Statement����SQL������ݿ�
			 stmt =conn.createStatement();
			stmt.executeUpdate(sql);
		}catch (Exception e) {
			e.printStackTrace();
			
		}finally{
			jdbcUtils.close(conn, stmt);
		
	}
	}*/


	

}


	



